package com.inossem.warehousestorage_inossem.activity.in_storage.chuangyou;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inStorage.chuangyou.CYShoppingInStorageListAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageShoppingListBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.chuangyou.CYInStorageShoppingListRequestBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.chuangyou.CYInStorgaeShoppingListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYInStorageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/27 19:39
 * @email siyu.wang@inossem.com
 */
public class CYInStorageShoppingListActivity extends BaseActivity{

    private TextView title;

        private PullToRefreshLayout refresh;

        private PullableListView materielList;

        private CYShoppingInStorageListAdapter adapter;

        private List<CYInStorageShoppingListBean.ShoppingListBean> allData;
        private String mCondition;

        @Override
        protected void onCreateImpl(Bundle savedInstanceState) {
            super.onCreateImpl(savedInstanceState);
            setContentView(R.layout.cy_activity_in_storage_shopping_list);
            initView(CYInStorageShoppingListActivity.this);
            initData(CYInStorageShoppingListActivity.this);
        }
    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
    }
    private void initData(Activity activity) {
        title.setText(R.string.cy_activity_in_storage_shopping_list_title);
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_receive_result_back), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        setmScanListener((String scanResult, int length) -> {
            mCondition = QRCodeUtils.DecryptQRCode(scanResult);
//            getData(activity, mCondition, mFactoryCode, mInventoryAddressCode, mBusinessType, mSpecialInventory);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
        resetData(activity);

    }
    private void resetData(Context context) {
        mCondition = "";
        allData = new ArrayList<>();
        addInStorageList(context , ActivityUtils.getShowData(allData, 0) ,materielList , true);
    }
    private void getData(Context context, String condition) {
        CYInStorageShoppingListRequestBean bean = CYInStorageUtils.getShoppingGetListRequestBean(condition);
        CYInStorgaeShoppingListRequest.request(context, bean, new GsonBeanListener<CYInStorageShoppingListBean>() {
            @Override
            public void onSuccess(CYInStorageShoppingListBean bean) {
                allData = bean.getBody();
                if (allData==null || allData.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_receive_list_no_message));
                }
                addInStorageList(context, ActivityUtils.getShowData(allData, 0), materielList ,true);
            }
        });
    }
    private void addInStorageList (Context context,List<CYInStorageShoppingListBean.ShoppingListBean> list , ListView listView, boolean flag) {
        if (flag) {
            adapter = new CYShoppingInStorageListAdapter(context, mCondition, list, new CYInStorageShoppingListener() {
                @Override
                public void onNoDoubleClickListener(String condition) {
                    mCondition = condition;
                    getData(context, mCondition);
                }
            });
            listView.setAdapter(adapter);
        }
    }
    public interface CYInStorageShoppingListener {
        void onNoDoubleClickListener(String condition);
    }
}
